﻿(function () {
    'use strict';
    var carouselController = function ($scope, $log, $routeParams, carouselFactory) {
        $scope.imageInfo = [];
        $scope.categories = [];

        function init() {
            carouselFactory.getCategories()
                .then(function (data) {
                    if (data) {
                        $scope.categories = data;
                        $scope.category = $scope.categories[0];

                        getImageInfo($scope.category.name);
                        $('.carousel').carousel();
                    }
                });

            if ($scope.category) {
                getImageInfo($scope.category);
            }
        }

        $scope.categoryChange = function () {
            //alert($scope.category.name);
            getImageInfo($scope.category.name);

        }

        init();

        var getImageInfo = function (category) {
            carouselFactory.getImageInfo(category)
            .then(function (data) {
                if (data) {
                    $scope.imageInfo = data;
                    angular.forEach($scope.imageInfo, function(ii) {
                        ii.url = window.escape(ii.url);
                    });
                }
            });
        }

    };

    carouselController.$inject = ['$scope', '$log', '$routeParams', 'carouselFactory'];
    angular.module('app')
        .controller('CarouselController', carouselController);
}());